/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository;

import cz.mzk.oai.metadataRepository.tools.CannotReadDirectoryException;
import cz.mzk.oai.metadataRepository.analysis.marcxml.MarcxmlFilesAnalyzer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import cz.mzk.oai.metadataRepository.repository.RecordAnalyzer;
import cz.mzk.oai.metadataRepository.harvest.MarcHarvester;
import cz.mzk.oai.metadataRepository.harvest.DirectoryHarvester;
import cz.mzk.oai.metadataRepository.metadata.MarcRecord;
import cz.mzk.oai.metadataRepository.repository.Repository;
import cz.mzk.oai.metadataRepository.tools.FileTools;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.DocumentException;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class Test {

    public static void downloadAndReadFromUrl(String url) {
        {
            BufferedReader in = null;
            try {
                URL yahoo = new URL(url);
                in = new BufferedReader(new InputStreamReader(yahoo.openStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println(inputLine);
                }
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    /**
     * @deprecated
     */
    public static void testDateTimeParser(String string) {
        //String string = "2010-03-13T08:02:05Z";
        DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
        DateTime dt = fmt.parseDateTime(string);
        System.out.println("dt: " + dt);
    }

    /**
     * @deprecated
     */
    public static void harvestDir(String srcDir, Properties properties, String log) {
        try {
            Repository repository = Repository.instanceOf(properties);
            MarcHarvester harvester = new DirectoryHarvester(srcDir, repository, log);
            harvester.harvestAndUpdate(null, null, true);
            harvester.close();
            repository.close();
        } catch (IOException ex) {
            System.err.println("Log file cannot be initialized");
            ex.printStackTrace();
            return;
        }
    }

    /**
     * @deprecated
     */
    public static void test651() {
        String logFile = "/home/martin/tmp/datafield651.log";
        MarcxmlFilesAnalyzer.datafield651Analysis("/home/martin/tmp/metadataCollections/unSorted/after1800/marc21", logFile);
        MarcxmlFilesAnalyzer.datafield651Analysis("/home/martin/tmp/metadataCollections/unSorted/before1800/marc21", logFile);
        MarcxmlFilesAnalyzer.datafield651Analysis("/home/martin/tmp/metadataCollections/unSorted/error/marc21", logFile);
    }

    /**
     * @deprecated
     */
    public static void copySchram(String sourceDir, String destDir) {
        File sourceDirFile = new File(sourceDir);
        if (!(sourceDirFile.exists() && sourceDirFile.isDirectory())) {
            System.err.println(sourceDir + "does not exist or is not directory");
            return;
        }
        File destDirFile = new File(destDir);
        if (!destDirFile.exists()) {
            destDirFile.mkdir();
        }
        List<String> filenames = null;
        try {
            filenames = FileTools.readFilenames(sourceDirFile);
        } catch (CannotReadDirectoryException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        RecordAnalyzer analyzer = new RecordAnalyzer();
        int counter = 0;
        int schramCounter = 0;
        System.out.println("Totaly files: " + filenames.size());
        try {
            for (String filename : filenames) {
                File sourceFile = new File(sourceDir + File.separator + filename);
                MarcRecord record = new MarcRecord(sourceFile);
                if (analyzer.isSchram(record)) {//kopirovat
                    File destFile = new File(destDir + File.separator + filename);
                    Runtime.getRuntime().exec("cp " + sourceFile + " " + destFile);
                    schramCounter++;

                }
                if (++counter % 1000 == 0) {
                    System.out.println("Processed files: " + counter);
                    System.out.println("Schram: " + schramCounter);
                }
            }
            System.out.println("Processed files: " + counter);
            System.out.println("Schram: " + schramCounter);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            System.err.println("Error while reading file");
        }
    }
}


